package com.jeff.android.lab;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.duff.download.okdownload.LiteDownload;
import com.duff.download.okdownload.interfaces.LiteDownloadListener;
import com.duff.download.okdownload.utils.MD5Utils;
import com.jack.network.NetworkManager;
import com.jeff.android.lab.architecture.eventbus.event.MessageEvent;
import com.jeff.android.lab.arouse.SkipTargetActivity;
import com.jeff.android.lab.module.widget.webview.WebViewFragment;
import com.jeff.android.lab.util.RouterUtil;
import com.jeff.permission.CheckPermission;
import com.jeff.permission.PermissionItem;
import com.jeff.permission.PermissionListener;
import com.jeff.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, MainFragment.instance())
                .commitAllowingStateLoss();

        EventBus.getDefault().register(this);

        CheckPermission.instance(this)
                .check(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE),
                        new PermissionListener() {
                            @Override
                            public void permissionGranted() {
                            }

                            @Override
                            public void permissionDenied() {
                            }
                        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE,  Menu.FIRST + 1 , 0, "设置");

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST + 1:
                Toast.makeText(MainActivity.this, "Option Click ... ", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NetworkManager.getDefault().unRegisterAllObservers();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMain(MessageEvent event) {

        Log.d(TAG, "EventBusFragment.onEventMain");

        if (event.id == 10008) {
            String result = (String) event.object;
            ToastUtil.show(this, result);

            if (result.startsWith("android-lab://url")) {
                final String realUrl = result.substring("android-lab://url".length() + 1);
                Bundle bundle = new Bundle();
                bundle.putString("data", realUrl);
                RouterUtil.launchFragmentActivity(this, WebViewFragment.class, bundle);
            } else if (result.startsWith("android-lab://target")) {
                RouterUtil.launchActivity(this, SkipTargetActivity.class);
            }
        }

        // 使用完移除粘性事件
        EventBus.getDefault().removeStickyEvent(event);
    }

}
